package com.fan.fan;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.ComponentName;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleAdapter;

public class MainList extends Activity {
	private ListView lv;
	private SimpleAdapter adapter;
	private List<Map<String, Object>> mList;
	private String[] items = { "检测手机信息", "音乐服务", "闹钟", "下载管理器", "动画效果",
			"虚席以待...", "虚席以待...", "虚席以待...", "虚席以待...", "虚席以待...", "虚席以待..." };
	private int[] img = { R.drawable.info, R.drawable.music, R.drawable.alarm,
			R.drawable.download, R.drawable.movie,
			R.drawable.ic_launcher, R.drawable.ic_launcher,
			R.drawable.ic_launcher, R.drawable.ic_launcher,
			R.drawable.ic_launcher, R.drawable.ic_launcher };

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.list);
		/*
		 * 从此往下为让程序显示在通知栏里的代码
		 */
		NotificationManager nm = (NotificationManager) this
				.getSystemService(NOTIFICATION_SERVICE);
		// 初始化通知
		Notification notification = new Notification(R.drawable.star, "",
				System.currentTimeMillis());
		Intent intent = new Intent(this, MainList.class);
		// intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
		// 设置点击通知时显示内容的类
		PendingIntent pendIntent = PendingIntent
				.getActivity(this, 0, intent, 0);
		// 设置通知显示的参数
		notification.setLatestEventInfo(this, "♂我的工具集合♀",
				"❤咳咳，我还活着呢，点击我就可以进入了哦❤", pendIntent);

		nm.notify(0, notification);// 执行通知.
		/*
		 * 从此网上为让程序显示在通知栏里的代码
		 */
		lv = (ListView) findViewById(R.id.list);
		initData();
		adapter = new SimpleAdapter(this, mList, R.layout.list_item,
				new String[] { "First", "Second" }, new int[] { R.id.iv,
						R.id.tv_item });
		lv.setAdapter(adapter);
		lv.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1,
					int position, long arg3) {
				// TODO Auto-generated method stub
				Intent it = new Intent();
				switch (position) {
				case 0:
					it.setClass(MainList.this, SplashViewAct.class);
					startActivity(it);
					break;
				case 1:
					it.setClass(MainList.this, MPlayer.class);
					startActivity(it);
					break;
				case 2:
					// 启动本地另外一个程序					
					ComponentName com = new ComponentName(
							"com.cn.daming.deskclock",
							"com.cn.daming.deskclock.DeskClockMainActivity");
					it.setComponent(com);
					startActivity(it);
					break;
				case 3:
					// it.setClass(MainList.this,
//					 DownLoadManagerActivity.class);
					// startActivity(it);
					break;
				case 4:
					it.setClass(MainList.this, AnimationTest.class);
					startActivity(it);
					break;
				}
			}
		});
		lv.setOnItemLongClickListener(new OnItemLongClickListener() {

			@Override
			public boolean onItemLongClick(AdapterView<?> arg0, View view,
					int position, long arg3) {
				// TODO Auto-generated method stub
				showMyDialog(position);
				return false;
			}

		});
	}

	private void initData() {
		// TODO Auto-generated method stub
		mList = new ArrayList<Map<String, Object>>();
		for (int i = 0; i < items.length; i++) {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("First", img[i]);
			map.put("Second", items[i]);
			mList.add(map);
		}
	}

	private void showMyDialog(final int position) {
		// TODO Auto-generated method stub
		LayoutInflater inflater = LayoutInflater.from(MainList.this);
		final View textEntryView = inflater.inflate(R.layout.alertview, null);

		new AlertDialog.Builder(this).setTitle("请输入更改后的名字")
				.setView(textEntryView)
				.setPositiveButton("确定", new OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						// TODO Auto-generated method stub

						// textEntryView.findViewById(R.id.alert_tv);
						EditText et = (EditText) textEntryView
								.findViewById(R.id.alert_tv);
						String addSort = et.getText().toString();
						items[position] = addSort;

					}
				}).create().show();
	}

}
